<%@page import="archsw0904.controller.catalog.*"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%
	request.setCharacterEncoding(application
			.getInitParameter("PARAMETER_ENCODING"));
%>
<jsp:include page="/partial/process_cache.jsp" />
<jsp:useBean id="usercache" scope="session"
	class="session.cache.UserCache" />
<%!private static final QueryableField[] FIELDS = { QueryableField.Isbn,
			QueryableField.Title, QueryableField.Author, QueryableField.Editor,
			QueryableField.Price, QueryableField.Year };
	private static final SearchOrder[] SEARCH_ORDERS = { SearchOrder.Ascending,
			SearchOrder.Descending, SearchOrder.Default };
	private static final String ORDER_BY = "orderBy";
	private static final String ORDER_TYPE = "orderType";

	private QueryParam getQueryParams(HttpServletRequest request) {
		List<QueryParamFieldsEntry> queryFields = new ArrayList<QueryParamFieldsEntry>();
		String param, orderParam, orderTypeParam;
		SearchOrder searchOrder, orderedFieldOrder;
		QueryableField orderedField;

		orderParam = request.getParameter(ORDER_BY);
		orderTypeParam = request.getParameter(ORDER_TYPE);
		orderedField = null;
		orderedFieldOrder = SearchOrder.Default;

		if (orderParam != null && orderParam.compareTo("") != 0) {
			for (QueryableField q : FIELDS) {
				if (orderParam.equalsIgnoreCase(q.getValue())) {
					orderedField = q;
				}
			}

			if (orderTypeParam != null && orderTypeParam.compareTo("") != 0) {
				for (SearchOrder s : SEARCH_ORDERS) {
					if (orderTypeParam.equalsIgnoreCase(s.getValue())) {
						orderedFieldOrder = s;
					}
				}
			}
		}

		for (QueryableField q : FIELDS) {

			param = request.getParameter(q.getValue().toLowerCase());
			if ((param != null) && (param.compareTo("") != 0)
					&& (q.equals(orderedField))) {
				queryFields
						.add(new QueryParamFieldsEntry(
								new QueryableFieldOrdered(orderedFieldOrder, q),
								param));
			} else if (q.equals(orderedField)) {
				queryFields.add(new QueryParamFieldsEntry(
						new QueryableFieldOrdered(orderedFieldOrder, q), null));
			} else if (param != null && param.compareTo("") != 0) {
				queryFields.add(new QueryParamFieldsEntry(
						new QueryableFieldOrdered(SearchOrder.Default, q),
						param));
			}
		}

		if (queryFields.size() != 0) {

			QueryParamFieldsEntry fields[] = new QueryParamFieldsEntry[queryFields
					.size()];
			return new QueryParam(queryFields.toArray(fields));
		} else {
			return null;
		}
	}%>
<%
	Book[] libri = null;
	Boolean searchSubmit = false;
	String errorMessage = null;
	QueryParam param;

	param = getQueryParams(request);
	if (param != null) {
		try {
			CatalogServiceLocator service = new CatalogServiceLocator();
			service.setCatalogPortEndpointAddress(application
					.getInitParameter("CATALOG_WEB_SERVICE_URI"));
			Catalog catalogService = service.getCatalogPort();
			libri = catalogService.queryBook(param);
		} catch (NullMandatoryFieldException e) {
			errorMessage = e.getField();
		}
		searchSubmit = true;
	}
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Search a Book <%
	if (usercache.getHostingNode() != null) {
%> @ <%=usercache.getHostingNode()%> <%
 	}
 %>
</title>
</head>
<body>
<jsp:include page="/partial/head_menu.jsp" />
<div style="float: left;">
<h1>Find what you need</h1>
<form action="/BookShopWeb/page/search.jsp" method="get">
<table>
	<tbody>
		<%
			for (QueryableField q : FIELDS) {
		%>
		<tr>
			<td><b><%=q.getValue()%></b></td>
			<td><input type="text" name="<%=q.getValue().toLowerCase()%>"
				size="20" /></td>
		</tr>
		<%
			}
		%>
	</tbody>
</table>
<input type="submit" value="Search" /></form>
<%
	if (searchSubmit) {
%> <%
 	if (libri == null) {
 %> <em>No result found</em> <%
 	} else {
 %>
<form>
<%
	for (QueryParamFieldsEntry f : param.getFields()) {
				if (f.getValue() != null) {
%> <input type="hidden"
	name="<%=f.getKey().getQueryField().getValue()
											.toLowerCase()%>"
	value="<%=f.getValue()%>"></input> <%
 	}
 			}
 %>
<p>Order by:</p>
<select name="<%=ORDER_BY%>">
	<%
		for (QueryableField q : FIELDS) {
	%>
	<option value="<%=q.getValue().toLowerCase()%>"><%=q.getValue()%></option>

	<%
		}
	%>
</select> <select name="<%=ORDER_TYPE%>">
	<%
		for (SearchOrder s : SEARCH_ORDERS) {
	%>
	<option value="<%=s.getValue().toLowerCase()%>"><%=s.getValue()%></option>

	<%
		}
	%>
</select> <input type="submit" value="Order!"></form>
<p><jsp:useBean id="booksCache" scope="request"
	class="session.cache.BookQueryCache" /> <%
 	booksCache.setBooks(libri);
 %> <jsp:include page="/partial/books_table.jsp" /> <%
 	}
 %>
</p>
<%
	if (errorMessage != null)
			out.println("<p><font color=\"red\"><b>Error: "
					+ errorMessage + "</b></font></p>");
%> <%
 	}
 %>
</div>
<%
	if (usercache.isClient()) {
%>
<div style="float: right;"><jsp:include
	page="/partial/cart_summary.jsp" /></div>
<%
	}
%>
</body>
</html>